package com.gitee.wsl.android.ui.crosstabdnd

//package it.niedermann.android.crosstabdnd;

import android.os.Handler
import androidx.recyclerview.widget.RecyclerView

class ScrollHelper : Runnable {
    enum class ScrollDirection {
        UP, DOWN
    }

    private var shouldScroll = false
    private var scrollDirection: ScrollDirection? = null
    private var currentRecyclerView: RecyclerView? = null
    private val handler = Handler()
    fun startScroll(recyclerView: RecyclerView, scrollDirection: ScrollDirection?) {
        this.scrollDirection = scrollDirection
        currentRecyclerView = recyclerView
        if (!shouldScroll) {
            shouldScroll = true
            handler.post(this)
        }
    }

    fun stopScroll() {
        shouldScroll = false
    }

    override fun run() {
        if (scrollDirection == ScrollDirection.UP) {
            currentRecyclerView?.smoothScrollBy(0, SCROLL_SPEED * -1)
        } else {
            currentRecyclerView?.smoothScrollBy(0, SCROLL_SPEED)
        }
        if (shouldScroll) {
            handler.postDelayed(this, 100)
        }
    }

    companion object {
        private const val SCROLL_SPEED = 200
    }
}